package com.banciyuan.bcywebview.biz.circles.smooth.viewholder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/4/14.
 */

public class GoodsHolder extends RecyclerView.ViewHolder {

    private View goodsLine;
    private TextView goods_count;
    private ImageView[] image_goods;
    private TextView[] goods_price;

    public GoodsHolder(View itemView) {
        super(itemView);
        goodsLine = itemView.findViewById(R.id.goods_line);
        goods_count = (TextView) itemView.findViewById(R.id.goods_count);
        goods_price = new TextView[]{(TextView) itemView.findViewById(R.id.goods_price_one), (TextView) itemView.findViewById(R.id.goods_price_two), (TextView) itemView.findViewById(R.id.goods_price_three)};
        image_goods = new ImageView[]{(ImageView) itemView.findViewById(R.id.goods_pic_one), (ImageView) itemView.findViewById(R.id.goods_pic_two), (ImageView) itemView.findViewById(R.id.goods_pic_three)};
    }

    public void renderGoods(final Context mContext, Timeline mItem) {
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
        itemView.setLayoutParams(layoutParams);
        List<DetailGoods> goods = mItem.getGoods_data();
        if (goods.size() <= 2) {
            ((View) (goodsLine.getParent())).setVisibility(View.GONE);
        } else {
            ((View) (goodsLine.getParent())).setVisibility(View.VISIBLE);
            for (int i = 0; i < goods.size(); i++) {
                if (i < 3) {
                    final String zg_id = goods.get(i).getZg_id();
                    goods_count.setText(String.format(mContext.getString(R.string.dongjin_goods), mItem.getGoods_count()));
                    ImageLoader.getInstance().displayImage(goods.get(i).getCover(), image_goods[i], ImageOptions.options);
                    goods_price[i].setText(String.format(mContext.getString(R.string.price_format), CurrencyUtil.getMoney(goods.get(i).getBasic_price())));
                    ((View)(goodsLine.getParent())).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BaseObserver.getInstance().notifyWatchers(BaseObserver.CIRCLE_GOGOODS);
                        }
                    });
                }
            }
        }

    }
}
